<?php
declare (strict_types=1);

namespace app\admin\validates;

use think\Validate;

/**
 * Menu验证器
 */
class OfficialAccountValidate extends Validate
{
    /**
     * 验证规则
     * @var array
     */
    protected $rule = [
        'id' => 'require|number',
        'name' => 'require|max:24',
        'messages_type' => 'requireIf:type,click|in:1,2,3,4,5,6',
        'type' => 'require|in:click,view,scancode_push,pic_weixin,location_select,scancode_waitmsg,pic_sysphoto,pic_photo_or_album,media_id,article_id,article_view_limited,miniprogram',
        'pid' => 'require|number',
        'sort' => 'require|number',
        'url' => 'requireIf:type,view|url',
        'content' => 'requireIf:messages_type,1',
        'key' => 'requireIf:type,click',
        'media_id' => 'requireCallback:check_media_require',
        'path' => 'requireCallback:check_path_require|url',
    ];

    /**
     * 验证字段描述
     * @var string[]
     */
    protected $field = [
        'id' => 'ID',
        'pid' => '上级ID',
        'name' => '菜单名称',
        'type' => '菜单类型',
        'sort' => '排序',
        'url' => '跳转链接',
        'content' => '回复内容',
        'messages_type' => '消息类型',
        'key' => '菜单key',
        'media_id' => '素材',
        'path' => '素材地址',
    ];

    /**
     * 验证场景
     * @var array
     */
    protected $scene = [
        'add' => ['name', 'type', 'pid', 'sort', 'url', 'messages_type', 'content', 'media_id', 'path'],
        'update' => ['id', 'name', 'type', 'pid', 'sort', 'url', 'messages_type', 'content', 'media_id', 'path'],
        'delete' => ['id'],
    ];

    /**
     * 验证素材
     * @param $value
     * @param $data
     * @return bool
     */
    public function check_media_require($value, $data): bool
    {
        if (in_array($data['messages_type'], [2, 3, 4, 5, 6])) {
            return true;
        }
        return false;
    }

    /**
     * 验证素材
     * @param $value
     * @param $data
     * @return bool
     */
    public function check_path_require($value, $data): bool
    {
        if (in_array($data['messages_type'], [2])) {
            return true;
        }
        return false;
    }
}